"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.globalStore = void 0;
const mobx_miniprogram_1 = require("mobx-miniprogram");
const env_1 = require("../utils/env");
const bootstrap_1 = require("../services/bootstrap");
exports.globalStore = (0, mobx_miniprogram_1.observable)({
    envId: (0, env_1.getDefaultEnvId)(),
    loading: false,
    role: "guest",
    userProfile: null,
    categories: [],
    menu: [],
    orders: [],
    cart: [],
    cartNote: "",
    activeOrderId: "",
    get isChef() {
        var _a;
        return ((_a = this.userProfile) === null || _a === void 0 ? void 0 : _a.role) === "chef";
    },
    get isDiner() {
        var _a;
        return ((_a = this.userProfile) === null || _a === void 0 ? void 0 : _a.role) === "diner";
    },
    setRole: (0, mobx_miniprogram_1.action)(function (role) {
        if (this.userProfile) {
            this.role = this.userProfile.role;
            return;
        }
        this.role = role;
    }),
    setUserProfile: (0, mobx_miniprogram_1.action)(function (profile) {
        var _a;
        this.userProfile = profile;
        this.role = (_a = profile === null || profile === void 0 ? void 0 : profile.role) !== null && _a !== void 0 ? _a : "guest";
    }),
    setCategories: (0, mobx_miniprogram_1.action)(function (list) {
        this.categories = list;
    }),
    setMenu: (0, mobx_miniprogram_1.action)(function (list) {
        this.menu = list;
    }),
    updateMenuSnapshot: (0, mobx_miniprogram_1.action)(function (item) {
        const index = this.menu.findIndex((menuItem) => menuItem._id === item._id);
        if (index >= 0) {
            this.menu[index] = item;
        }
        else {
            this.menu.unshift(item);
        }
    }),
    removeMenuSnapshot: (0, mobx_miniprogram_1.action)(function (menuId) {
        this.menu = this.menu.filter((item) => item._id !== menuId);
    }),
    setOrders: (0, mobx_miniprogram_1.action)(function (list) {
        this.orders = list;
    }),
    updateOrderSnapshot: (0, mobx_miniprogram_1.action)(function (order) {
        const index = this.orders.findIndex((item) => item._id === order._id);
        if (index >= 0) {
            this.orders[index] = order;
        }
        else {
            this.orders.unshift(order);
        }
    }),
    setActiveOrder: (0, mobx_miniprogram_1.action)(function (orderId) {
        this.activeOrderId = orderId;
    }),
    setCart: (0, mobx_miniprogram_1.action)(function (items) {
        this.cart = items;
    }),
    setCartNote: (0, mobx_miniprogram_1.action)(function (note) {
        this.cartNote = note;
    }),
    clearCart: (0, mobx_miniprogram_1.action)(function () {
        this.cart = [];
        this.cartNote = "";
    }),
    logout: (0, mobx_miniprogram_1.action)(function () {
        this.userProfile = null;
        this.role = "guest";
        this.categories = [];
        this.menu = [];
        this.orders = [];
        this.cart = [];
        this.cartNote = "";
        this.activeOrderId = "";
    }),
    initAppData: (0, mobx_miniprogram_1.action)(function () {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b, _c, _d, _e;
            if (this.loading)
                return;
            this.loading = true;
            try {
                const data = yield (0, bootstrap_1.fetchBootstrap)();
                this.envId = data.envId || this.envId;
                this.userProfile = data.userProfile;
                this.role = (_b = (_a = data.userProfile) === null || _a === void 0 ? void 0 : _a.role) !== null && _b !== void 0 ? _b : "guest";
                this.categories = (_c = data.categories) !== null && _c !== void 0 ? _c : [];
                this.menu = (_d = data.menu) !== null && _d !== void 0 ? _d : [];
                this.orders = (_e = data.orders) !== null && _e !== void 0 ? _e : [];
            }
            catch (error) {
                console.error("初始化数据失败", error);
            }
            finally {
                this.loading = false;
            }
        });
    }),
});
